package io.renren.modules.app.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import io.renren.common.utils.R;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.service.AppWebServiceService;
import io.renren.webserviceclient.nc.itf.uap.pf.IPFMobiletoWebService.IPFMobiletoWebServiceSOAP11BindingStub;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.*;

@Data
@Service("appWebServiceService")
@ConfigurationProperties(prefix = "nc")
public class AppWebServiceServiceImpl  implements AppWebServiceService {

	private String IPCONFIG; //10.201.185.39

	@Autowired
	private UserDao userDao;

	@Override
	public R getTaskList(Map<String,Object> data) {
		try {
			/*String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.getTaskList(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});*/
			String billtype = data.get("billtype")+"";
			String newstatuscode = null;
			if(data.get("statuscode").equals("unhandled")){
				newstatuscode = "0";
			}else{
				newstatuscode = "1";
			}
			int  cur =  Integer.parseInt(data.get("currPage").toString());//当前页
			int  size =  Integer.parseInt(data.get("pageSize").toString() );//一页的记录数
			int anum ;//每页的第一条数据的行号
			int wnum = cur*size; //每页的最后一条数据的行号
			if(cur == 1) {
				anum=1;
			} else {
				anum = (cur-1)*size+1;
			}
			Map maps = new HashMap();
			maps.put("checkman",data.get("userid"));
			maps.put("approvestatus",newstatuscode);
			maps.put("wnum",wnum);
			maps.put("anum",anum);
			List<Map<String, Object>> tasks = null;
			if(billtype.equals("36J5")){
				String[] billtyps = new String[2];
				billtyps[0] = "36J5";
				billtyps[1] = "36J1";
				maps.put("billtype",billtyps);
				tasks = userDao.getTaskids(maps);
			}else if (billtype.equals("36J2")){
				String[] billtyps = new String[2];
				billtyps[0] = "36J2";
				billtyps[1] = "36J7";
				maps.put("billtype",billtyps);
				tasks = userDao.getTaskids(maps);
			}else{
				String[] billtyps = new String[1];
				billtyps[0] = data.get("billtype")+"";
				maps.put("billtype",billtyps);
				tasks = userDao.getTaskids(maps);
			}

			//Object flag = retmsg.get(0).get("flag");
			/*if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else*/  if (tasks.size() > 0){
				/*List<Map<String, Object>> taskstructlist = (List<Map<String, Object>>) retmsg.get(0).get("taskstructlist");
				if(taskstructlist.size()==0){
					return  R.error().put("data","查询无数据");
				}*/
				String[] taskids = new String[tasks.size()];
				for (int i = 0; i < tasks.size(); i++) {
					taskids[i] = tasks.get(i).get("PK_CHECKFLOW")+"";
				}
				Map map = new HashMap();
				map.put("array",taskids);
				map.put("wnum",wnum);
				map.put("anum",anum);
				if(billtype.equals("D5")){
					map.put("billtype",billtype);
					List<Map<String, Object>>  taskList =userDao.getTaskList(map);
					System.out.println("taskList====="+taskList);
					return R.ok().put("data", taskList);
				}else if(billtype.equals("36J5")){
					String[] billtypes = new String[2];
					billtypes[0] = "36J5";
					billtypes[1] = "36J1";
					map.put("billtypes",billtypes);
					List<Map<String, Object>>  taskList =userDao.getEntrustList(map);
					System.out.println("taskList====="+taskList);
					return R.ok().put("data", taskList);
				}else if(billtype.equals("D4")){
					map.put("billtype",billtype);
					List<Map<String, Object>>  taskList =userDao.getCollectList(map);
					System.out.println("taskList====="+taskList);
					return R.ok().put("data", taskList);
				}else if(billtype.equals("36J2")){
					String[] billtypes = new String[2];
					billtypes[0] = "36J2";
					billtypes[1] = "36J7";
					map.put("billtypes",billtypes);
					List<Map<String, Object>>  taskList =userDao.getEntrustGatherList(map);
					System.out.println("taskList====="+taskList);
					return R.ok().put("data", taskList);
				}else if(billtype.equals("36J3")){
					map.put("billtype",billtype);
					List<Map<String, Object>>  taskList =userDao.getInterTransferList(map);
					System.out.println("taskList====="+taskList);
					return R.ok().put("data", taskList);
				}else if(billtype.equals("36S4")){
					map.put("billtype",billtype);
					List<Map<String, Object>>  taskList =userDao.getTransfersetList(map);
					System.out.println("taskList====="+taskList);
					return R.ok().put("data", taskList);
				}else if(billtype.equals("36K4")){
					map.put("billtype",billtype);
					List<Map<String, Object>>  taskList =userDao.getFunCollectList(map);
					System.out.println("taskList====="+taskList);
					return R.ok().put("data", taskList);
				}else if(billtype.equals("36K2")){
					map.put("billtype",billtype);
					List<Map<String, Object>>  taskList =userDao.getFundAllocaList(map);
					System.out.println("taskList====="+taskList);
					return R.ok().put("data", taskList);
				}
			}else{
				return  R.ok().put("data", new ArrayList<String>());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R getTaskBill(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.getTaskBill(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				System.out.println("statusstructlist="+retmsg.get(0).get("taskstructlist"));
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R getTaskAction(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.getTaskAction(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R doAgree(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.doAgree(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R doDisAgree(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.doDisAgree(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R doReject(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.doReject(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R getUserList(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.getUserList(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				System.out.println("statusstructlist="+retmsg.get(0).get("psnstructlist"));
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R getApprovedDetail(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.getApprovedDetail(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				String billno = retmsg.get(0).get("billname")+"";
				List<Map<String,Object>> list =  (List<Map<String,Object>>)retmsg.get(0).get("approvehistorylinelist");
				Iterator<Map<String,Object>> it = list.iterator();
				while (it.hasNext()){
					if(it.next().get("note") == null){
						it.remove();
					}
				}
				if(retmsg.get(0).get("billtypename").equals("付款结算单")) {
					Map map = userDao.getBillDetail(billno);
					retmsg.get(0).put("billtype", "D5");
					retmsg.get(0).put("detail", map);
				}else if(retmsg.get(0).get("billtypename").equals("合并委托付款书") || retmsg.get(0).get("billtypename").equals("委托付款书")){
					retmsg.get(0).put("approvehistorylinelist", list);
					Map map = userDao.getProvallDetail(billno);
					if(retmsg.get(0).get("billtypename").equals("合并委托付款书")){
						retmsg.get(0).put("billtype", "36J5");
					}else if(retmsg.get(0).get("billtypename").equals("委托付款书")){
						retmsg.get(0).put("billtype", "36J1");
					}
					retmsg.get(0).put("detail", map);
				}else if(retmsg.get(0).get("billtypename").equals("收款结算单")){
					Map map = userDao.getCollectDetail(billno);
					retmsg.get(0).put("billtype", "D4");
					retmsg.get(0).put("detail", map);
				}else if(retmsg.get(0).get("billtypename").equals("合并委托收款书") || retmsg.get(0).get("billtypename").equals("委托收款书")){
					retmsg.get(0).put("approvehistorylinelist", list);
					Map map = userDao.getEntrustGatherDetail(billno);
					if(retmsg.get(0).get("billtypename").equals("合并委托收款书")){
						retmsg.get(0).put("billtype", "36J7");
					}else if(retmsg.get(0).get("billtypename").equals("委托收款书")){
						retmsg.get(0).put("billtype", "36J2");
					}
					retmsg.get(0).put("detail", map);
				}else if(retmsg.get(0).get("billtypename").equals("内部转账单")){
					retmsg.get(0).put("approvehistorylinelist", list);
					Map map = userDao.getInterTransferDetail(billno);
					retmsg.get(0).put("billtype", "36J3");
					retmsg.get(0).put("detail", map);
				}else if(retmsg.get(0).get("billtypename").equals("划账结算")) {
					Map map = userDao.getTransfersetDetail(billno);
					retmsg.get(0).put("billtype", "36S4");
					retmsg.get(0).put("detail", map);
				}else if(retmsg.get(0).get("billtypename").equals("上收单")) {
					Map map = userDao.getFunCollectDetail(billno);
					retmsg.get(0).put("billtype", "36K4");
					retmsg.get(0).put("detail", map);
				}else if(retmsg.get(0).get("billtypename").equals("下拨单")) {
					Map map = userDao.getFundAllocaDetail(billno);
					retmsg.get(0).put("billtype", "36K2");
					retmsg.get(0).put("detail", map);
				}
				return R.ok().put("data", retmsg.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R getPsnDetail(Map<String, Object> data) {
		//经过测试，NC接口存在故障
		String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
		try {
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.getPsnDetail(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			}
			if ("0".equals(flag)){
				System.out.println("statusstructlist="+retmsg.get(0).get("statusstructlist"));
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R getTaskStatusList(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.getTaskStatusList(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R getTaskButtonList(Map<String, Object> data) {
		String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
		try {
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String status = data.get("status")+"";
			String res = stub.getTaskButtonList(JSON.toJSONString(status));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			}
			if ("0".equals(flag)){
				System.out.println("statusstructlist="+retmsg.get(0).get("statusstructlist"));
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R getDefaultValueOfAction(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.getDefaultValueOfAction(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				return R.ok().put("data", retmsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}

	@Override
	public R doAction(Map<String, Object> data) {
		try {
			String endpoint =  IPCONFIG+"uapws/service/nc.itf.uap.pf.IPFMobiletoWebService";
			IPFMobiletoWebServiceSOAP11BindingStub stub = new IPFMobiletoWebServiceSOAP11BindingStub(new URL(endpoint), new org.apache.axis.client.Service());
			String res = stub.doAction(JSON.toJSONString(data));
			List<Map<String, Object>> retmsg= JSON.parseObject(res, new TypeReference<List<Map<String, Object>>>(){});
			Object flag = retmsg.get(0).get("flag");
			if ("1".equals(flag)) {
				return  R.error().put("data",retmsg.get(0).get("des"));
			} else  if ("0".equals(flag)){
				return R.ok().put("data", retmsg.get(0).get("des"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  R.error().put("data", e.toString());
		}
		return R.error().put("data", "获取数据异常");
	}
}
